//
//  SearchResidencePermitViewController.m
//  Jyxt
//
//  Created by 宫璞 on 2017/8/18.
//  Copyright © 2017年 DaLian Rental Housing Management. All rights reserved.
//

#import "SearchResidencePermitViewController.h"
#import "SearchResidencePermitViewModel.h"
#import <LemonBubble.h>
#import "UserCenterViewModel.h"
#import "QueryResultResidencePermitViewController.h"
#define GETWIDTH       [UIScreen mainScreen].bounds.size.width
#define GETHEIGHT      [UIScreen mainScreen].bounds.size.height

@interface SearchResidencePermitViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>{
     NSDate *contractStartTime;
    NSDate *contracEndTime;
    NSMutableArray *cityNameArray;
    NSMutableArray *policeArray;
    
    UIPickerView *cityPickerView;
    NSDictionary *fanhuiDic;
    NSDictionary *policeFanhuiDic;
    UIPickerView *policePickerView;
    UIPickerView *conditionPickerView;
    NSArray *conditionArray;
    NSDictionary *conditionDic;
    NSString *conditionString;
    NSMutableDictionary *valueDic;
}
@property (nonatomic, strong) UIScrollView *allScrollview;
@property NSArray *arrayO;

//请选择筛选条件
@property (nonatomic, strong) UILabel *chooseConditionLabel;
@property (nonatomic, strong) UIView *firstView;
@property (nonatomic, strong) UILabel *startTimeLabel;
@property (nonatomic, strong) UILabel *endTimeLabel;
@property (nonatomic, strong) UILabel *firstGrayLinelabel;
@property (nonatomic, strong) UILabel *chooseStartTimeLabel;
@property (nonatomic, strong) UILabel *chooseEndTimeLabel;

@property (nonatomic, strong) UIView *secondView;
@property (nonatomic, strong) UILabel *cityLabel;
@property (nonatomic, strong) UILabel *policeLabel;
@property (nonatomic, strong) UILabel *chooseCitylabel;
@property (nonatomic, strong) UILabel *choosePoliceLabel;
@property (nonatomic, strong) UILabel *secondGrayLineLabel;

@property (nonatomic, strong) UIView *thirdView;
@property (nonatomic, strong) UILabel *checkStatusLabel;
@property (nonatomic, strong) UILabel *chooseCheckStatusLabel;

@property (nonatomic, strong) UIView *fourthView;
@property (nonatomic, strong) UILabel *applyConditionLabel;
@property (nonatomic, strong) UILabel *chooseApplyConditionLabel;
@property (nonatomic, strong) UILabel *fourthGrayLinelabel;
@property (nonatomic, strong) UILabel *houseAffiliationLabel;
@property (nonatomic, strong) UILabel *chooseHouseAffiliationLabel;

@property (nonatomic, strong) UIView *fifthView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UITextField *nameTextfield;
@property (nonatomic, strong) UILabel *indentityNumberLabel;
@property (nonatomic, strong) UITextField *indentityNumberTextfield;
@property (nonatomic, strong) UILabel *fifthGraylineLabel;

@property (nonatomic, strong) UIButton *submitButton;
@property (nonatomic, strong) UIButton *resetButton;

@end

@implementation SearchResidencePermitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)initView{
    self.title = @"申请信息查询";
    
    self.allScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    self.allScrollview.backgroundColor = LKColorMake(245, 245, 245, 1);
    self.allScrollview.contentSize = CGSizeMake(0, 600);
    [self addSubview:self.allScrollview];
    
    self.chooseConditionLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 5, 100, 10)];
    self.chooseConditionLabel.text = @"请选择筛选条件";
    self.chooseConditionLabel.font = [UIFont systemFontOfSize:13];
    self.chooseConditionLabel.textColor = LKColorMake(157, 157, 157, 1);
    [self.allScrollview addSubview:self.chooseConditionLabel];
    
    //第一个 view 中内容
    self.firstView = [[UIView alloc]initWithFrame:CGRectMake(0, self.chooseConditionLabel.frame.origin.y + self.chooseConditionLabel.frame.size.height + 10, [UIScreen mainScreen].bounds.size.width, 88)];
    self.firstView.backgroundColor = [UIColor whiteColor];
    [self.allScrollview addSubview:self.firstView];
    
    self.startTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 12, 80, 20)];
    self.startTimeLabel.text = @"起始时间";
    self.startTimeLabel.font = [UIFont systemFontOfSize:15];
    [self.firstView addSubview:self.startTimeLabel];
    
    self.chooseStartTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.startTimeLabel.frame.origin.x + self.startTimeLabel.frame.size.width + 10, 12, 200, 20)];
    self.chooseStartTimeLabel.text = @"请选择";
    self.chooseStartTimeLabel.font = [UIFont systemFontOfSize:15];
    self.chooseStartTimeLabel.textColor = LKColorMake(188, 188, 188, 1);
    self.chooseStartTimeLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chooseStartTimeAction)];
    [self.chooseStartTimeLabel addGestureRecognizer:gesture];
    [self.firstView addSubview:self.chooseStartTimeLabel];
    
    self.firstGrayLinelabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 43.5, GETWIDTH - 15, 0.5)];
    self.firstGrayLinelabel.backgroundColor = LKColorMake(230, 230, 230, 1);
    [self.firstView addSubview:self.firstGrayLinelabel];
    
    self.endTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, self.firstGrayLinelabel.frame.origin.y + self.firstGrayLinelabel.frame.size.height + 12, 80, 20)];
    self.endTimeLabel.text = @"结束时间";
    self.endTimeLabel.font = [UIFont systemFontOfSize:15];
    [self.firstView addSubview:self.endTimeLabel];
    
    self.chooseEndTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.endTimeLabel.frame.origin.x + self.endTimeLabel.frame.size.width + 10, self.firstGrayLinelabel.frame.origin.y + self.firstGrayLinelabel.frame.size.height + 12, 200, 20)];
    self.chooseEndTimeLabel.text = @"请选择";
    self.chooseEndTimeLabel.font = [UIFont systemFontOfSize:15];
    self.chooseEndTimeLabel.textColor = LKColorMake(188, 188, 188, 1);
    self.chooseEndTimeLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *chooseEndTimeGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chooseEndTimeAction)];
    [self.chooseEndTimeLabel addGestureRecognizer:chooseEndTimeGesture];
    [self.firstView addSubview:self.chooseEndTimeLabel];
    
    
    //第二个 view 中内容
    self.secondView = [[UIView alloc]initWithFrame:CGRectMake(0, self.firstView.frame.origin.y + self.firstView.frame.size.height + 10, GETWIDTH, 88)];
    self.secondView.backgroundColor = [UIColor whiteColor];
    [self.allScrollview addSubview:self.secondView];
    
    self.cityLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 12, 80, 20)];
    self.cityLabel.text = @"所属行政区";
    self.cityLabel.font = [UIFont systemFontOfSize:15];
    [self. secondView addSubview:self.cityLabel];
    
    self.chooseCitylabel = [[UILabel alloc]initWithFrame:CGRectMake(self.cityLabel.frame.origin.x + self.cityLabel.frame.size.width + 10, 12, 200, 20)];
    self.chooseCitylabel.text = @"请选择";
    self.chooseCitylabel.font = [UIFont systemFontOfSize:15];
    self.chooseCitylabel.textColor = LKColorMake(188, 188, 188, 1);
    self.chooseCitylabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *chooseCityGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chooseCityAction)];
    [self.chooseCitylabel addGestureRecognizer:chooseCityGesture];
    [self.secondView addSubview:self.chooseCitylabel];
    
    self.secondGrayLineLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 43.5, GETWIDTH - 15, 0.5)];
    self.secondGrayLineLabel.backgroundColor = LKColorMake(230, 230, 230, 1);
    [self.secondView addSubview:self.secondGrayLineLabel];
    
    self.policeLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, self.secondGrayLineLabel.frame.origin.y + self.secondGrayLineLabel.frame.size.height + 12, 80, 20)];
    self.policeLabel.text = @"所属派出所";
    self.policeLabel.font = [UIFont systemFontOfSize:15];
    [self.secondView addSubview:self.policeLabel];
    
    self. choosePoliceLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.policeLabel.frame.origin.x + self.policeLabel.frame.size.width + 10, self.secondGrayLineLabel.frame.origin.y + self.secondGrayLineLabel.frame.size.height + 12, 200, 20)];
    self.choosePoliceLabel.text = @"请选择";
    self.choosePoliceLabel.font = [UIFont systemFontOfSize:15];
    self.choosePoliceLabel.textColor = LKColorMake(188, 188, 188, 1);
    self.choosePoliceLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *choosePoliceGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(choosePoliceAction)];
    [self.choosePoliceLabel addGestureRecognizer:choosePoliceGesture];
    [self.secondView addSubview:self.choosePoliceLabel];
    
    //第三个 view 中内容
    self. thirdView = [[UIView alloc]initWithFrame:CGRectMake(0, self.secondView.frame.origin.y + self.secondView.frame.size.height + 10, GETWIDTH, 44)];
    self.thirdView.backgroundColor = [UIColor whiteColor];
    [self.allScrollview addSubview:self.thirdView];
    
    self.checkStatusLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 12, 80, 20)];
    self.checkStatusLabel.text = @"审核状态";
    self.checkStatusLabel.font = [UIFont systemFontOfSize:15];
    [self.thirdView addSubview:self.checkStatusLabel];
    
    self.chooseCheckStatusLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.checkStatusLabel.frame.origin.x + self.checkStatusLabel.frame.size.width + 10, 12, GETWIDTH - 45, 20)];
    self.chooseCheckStatusLabel.text = @"请选择";
    self.chooseCheckStatusLabel.font = [UIFont systemFontOfSize:15];
    self.chooseCheckStatusLabel.textColor = LKColorMake(188, 188, 188, 1);
    self.chooseCheckStatusLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *chooseCheckStatusGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chooseCheckStatusAction)];
    [self.chooseCheckStatusLabel addGestureRecognizer:chooseCheckStatusGesture];
    [self.thirdView addSubview:self.chooseCheckStatusLabel];
    
    //第四个 view 中内容
    self. fourthView = [[UIView alloc]initWithFrame:CGRectMake(0, self.thirdView.frame.origin.y + self.thirdView.frame.size.height + 10, GETWIDTH, 88)];
    self.fourthView.backgroundColor = [UIColor whiteColor];
    [self.allScrollview addSubview:self.fourthView];
    
    self. applyConditionLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 12, 80, 20)];
    self.applyConditionLabel.text = @"申请条件";
    self.applyConditionLabel.font = [UIFont systemFontOfSize:15];
    [self. fourthView addSubview:self.applyConditionLabel];
    
    self.chooseApplyConditionLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.applyConditionLabel.frame.origin.x + self.applyConditionLabel.frame.size.width + 10, 12, 200, 20)];
    self.chooseApplyConditionLabel.text = @"请选择";
    self.chooseApplyConditionLabel.font = [UIFont systemFontOfSize:15];
    self.chooseApplyConditionLabel.textColor = LKColorMake(188, 188, 188, 1);
    self.chooseApplyConditionLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *chooseApplyConditionGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chooseApplyConditionAction)];
    [self.chooseApplyConditionLabel addGestureRecognizer:chooseApplyConditionGesture];
    [self.fourthView addSubview:self.chooseApplyConditionLabel];
    
    self.fourthGrayLinelabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 43.5, GETWIDTH - 15, 0.5)];
    self.fourthGrayLinelabel.backgroundColor = LKColorMake(230, 230, 230, 1);
    [self.fourthView addSubview:self.fourthGrayLinelabel];
    
    self.houseAffiliationLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, self.fourthGrayLinelabel.frame.origin.y + self.fourthGrayLinelabel.frame.size.height + 12, 80, 20)];
    self.houseAffiliationLabel.text = @"房屋权属";
    self.houseAffiliationLabel.font = [UIFont systemFontOfSize:15];
    [self.fourthView addSubview:self.houseAffiliationLabel];
    
    self. chooseHouseAffiliationLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.houseAffiliationLabel.frame.origin.x + self.houseAffiliationLabel.frame.size.width + 10, self.fourthGrayLinelabel.frame.origin.y + self.fourthGrayLinelabel.frame.size.height + 12, 200, 20)];
    self.chooseHouseAffiliationLabel.text = @"请选择";
    self.chooseHouseAffiliationLabel.font = [UIFont systemFontOfSize:15];
    self.chooseHouseAffiliationLabel.textColor = LKColorMake(188, 188, 188, 1);
    self.chooseHouseAffiliationLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *chooseHouseAffiliationGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chooseHouseAffiliationAction)];
    [self.chooseHouseAffiliationLabel addGestureRecognizer:chooseHouseAffiliationGesture];
    [self.fourthView addSubview:self.chooseHouseAffiliationLabel];
    
    //第四个 view 中内容
    self. fifthView = [[UIView alloc]initWithFrame:CGRectMake(0, self.fourthView.frame.origin.y + self.fourthView.frame.size.height + 15, GETWIDTH, 88)];
    self.fifthView.backgroundColor = [UIColor whiteColor];
    [self.allScrollview addSubview:self.fifthView];
    
    self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 12, 80, 20)];
    self.nameLabel.text = @"姓名";
    self.nameLabel.font = [UIFont systemFontOfSize:15];
    [self.fifthView addSubview:self.nameLabel];
    
    
    self.nameTextfield = [[UITextField alloc]initWithFrame:CGRectMake(self.nameLabel.frame.origin.x + self.nameLabel.frame.size.width + 10, 12, [UIScreen mainScreen].bounds.size.width - 80 - 10 - 15, 20)];
    self.nameTextfield.placeholder = @"请输入";
    self.nameTextfield.clearButtonMode = UITextFieldViewModeAlways;
    [self.fifthView addSubview:self.nameTextfield];
    
    self.fifthGraylineLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 43.5, GETWIDTH - 15, 0.5)];
    self.fifthGraylineLabel.backgroundColor = LKColorMake(230, 230, 230, 1);
    [self.fifthView addSubview:self.fifthGraylineLabel];
    
    self.indentityNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, self.fifthGraylineLabel.frame.origin.y + self.fifthGraylineLabel.frame.size.height + 12, 80, 20)];
    self.indentityNumberLabel.text = @"身份证号";
    self.indentityNumberLabel.font = [UIFont systemFontOfSize:15];
    [self.fifthView addSubview:self.indentityNumberLabel];
    
    self.indentityNumberTextfield = [[UITextField alloc]initWithFrame:CGRectMake(self.indentityNumberLabel.frame.origin.x + self.indentityNumberLabel.frame.size.width + 10, self.fifthGraylineLabel.frame.origin.y + self.fifthGraylineLabel.frame.size.height + 12, GETWIDTH - 80 - 10 - 15, 20)];
    self.indentityNumberTextfield.placeholder = @"请输入";
    self.indentityNumberTextfield.clearButtonMode = UITextFieldViewModeAlways;
    [self.fifthView addSubview:self.indentityNumberTextfield];
    
    self.submitButton = [[UIButton alloc]initWithFrame:CGRectMake(15, self.fifthView.frame.origin.y + self.fifthView.frame.size.height + 20, GETWIDTH - 30, 44)];
    self.submitButton.backgroundColor = LKColorMake(81, 117, 225, 1);
    self.submitButton.layer.cornerRadius = 5;
    [self.submitButton setTitle:@"提交" forState: UIControlStateNormal];
    [self.submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.submitButton addTarget:self action:@selector(submitButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.allScrollview addSubview:self.submitButton];
    
    self.resetButton = [[UIButton alloc]initWithFrame:CGRectMake(15, self.submitButton.frame.origin.y + self.submitButton.frame.size.height + 20, GETWIDTH - 30, 44)];
    self.resetButton.backgroundColor = LKColorMake(81, 117, 225, 1);
    self.resetButton.layer.cornerRadius = 5;
    self.resetButton.layer.borderWidth = 0.5;
    self.resetButton.layer.borderColor = LKColorMake(245, 245, 245, 1).CGColor;
    [self.resetButton setTitle:@"重置查询条件" forState: UIControlStateNormal];
    self.resetButton.backgroundColor = [UIColor whiteColor];
    [self.resetButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.resetButton addTarget:self action:@selector(resetButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.allScrollview addSubview:self.resetButton];
    
    valueDic = [[NSMutableDictionary alloc]initWithDictionary:@{@"areacode":@"",@"status":@"",@"begintime":@"",@"endtime":@"",@"idno":@"",@"name":@"",@"dwellingtype":@"",@"condition":@"",@"pcscode":@""}];
}

//选择起始时间
- (void)chooseStartTimeAction{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle: nil message: @"请选择起始的时间\n\n\n\n\n\n\n\n\n"  preferredStyle: UIAlertControllerStyleActionSheet];
    UIView *uv_pickerBG = [[UIView alloc]initWithFrame:CGRectMake(0, 40, GETWIDTH-20, 170)];
    UIDatePicker *udp_startTime = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, GETWIDTH-20, 180)];
    udp_startTime.tag = 10000;
    udp_startTime.backgroundColor = [UIColor groupTableViewBackgroundColor];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//设置为中文显示
    udp_startTime.locale = locale;
    //当前时间创建NSDate
    NSDate *localDate = [NSDate date];
    contractStartTime = [localDate dateByAddingTimeInterval:(8*60*60)];
    udp_startTime.maximumDate = localDate;
    udp_startTime.datePickerMode = UIDatePickerModeDate;
    [udp_startTime addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
    [uv_pickerBG addSubview:udp_startTime];
    [alert.view addSubview: uv_pickerBG];
    [alert addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler: nil]];
    [alert addAction: [UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([self compareOneDay:contracEndTime withAnotherDay:contractStartTime] == -1) {
            NSLog(@"时间错误");
        }else{
            
            self.chooseStartTimeLabel.text = [[NSString stringWithFormat:@"%@",contractStartTime] substringToIndex:10];
            self.chooseStartTimeLabel.textColor = [UIColor blackColor];
            [valueDic setValue:[[NSString stringWithFormat:@"%@",contractStartTime] substringToIndex:10] forKey:@"begintime"];
        }
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}
- (void)dateChanged:(id)sender{
    UIDatePicker *control = (UIDatePicker *)sender;
    if (control.tag == 10000) {
        contractStartTime = [control.date dateByAddingTimeInterval:(8*60*60)];
        
    }else{
        contracEndTime = [control.date dateByAddingTimeInterval:(8*60*60)];
    }
}

- (NSInteger)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
    NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
    NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
    NSComparisonResult result = [dateA compare:dateB];
    if (result == NSOrderedDescending) {
        //NSLog(@"oneDay比 anotherDay时间晚");
        return 1;
    }
    else if (result == NSOrderedAscending){
        //NSLog(@"oneDay比 anotherDay时间早");
        return -1;
    }
    //NSLog(@"两者时间是同一个时间");
    return 0;
}


//选择结束时间
- (void)chooseEndTimeAction{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle: nil message: @"请选择起始的时间\n\n\n\n\n\n\n\n\n"  preferredStyle: UIAlertControllerStyleActionSheet];
    UIView *uv_pickerBG = [[UIView alloc]initWithFrame:CGRectMake(0, 40, GETWIDTH-20, 170)];
    UIDatePicker *udp_startTime = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, GETWIDTH-20, 180)];
    udp_startTime.tag = 10001;
    udp_startTime.backgroundColor = [UIColor groupTableViewBackgroundColor];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//设置为中文显示
    udp_startTime.locale = locale;
    //当前时间创建NSDate
    NSDate *localDate = [NSDate date];
    contracEndTime = [localDate dateByAddingTimeInterval:(8*60*60)];
    udp_startTime.datePickerMode = UIDatePickerModeDate;
    [udp_startTime addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
    [uv_pickerBG addSubview:udp_startTime];
    [alert.view addSubview: uv_pickerBG];
    [alert addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler: nil]];
    [alert addAction: [UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([self compareOneDay:contracEndTime withAnotherDay:contractStartTime] == -1) {
            NSLog(@"时间错误");
            [self showErrorWithTitle:@"结束时间不能小于起始时间" autoCloseTime:2];
        }else{
            self.chooseEndTimeLabel.text = [[NSString stringWithFormat:@"%@",contracEndTime] substringToIndex:10];
            self.chooseEndTimeLabel.textColor = [UIColor blackColor];
            [valueDic setValue:[[NSString stringWithFormat:@"%@",contracEndTime] substringToIndex:10] forKey:@"endtime"];
        }
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}

//选择所属行政区
- (void)chooseCityAction{
    cityNameArray = [[NSMutableArray alloc]init];
    [SearchResidencePermitViewModel searchResidencePermitWhenSuccess:^(id responseObject) {
        [cityNameArray addObjectsFromArray:responseObject[@"data"]];
        [cityNameArray insertObject:@{@"code":@"",@"name":@"无限制"} atIndex:0];
        [cityPickerView reloadAllComponents];
        fanhuiDic = cityNameArray[0];
    } failed:^(NSError *error) {
    }];
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle: nil message: @"请选择所属行政区\n\n\n\n\n\n\n\n\n"  preferredStyle: UIAlertControllerStyleActionSheet];
    cityPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, GETWIDTH-20, 180)];
    // 显示选中框
    cityPickerView.tag = 10086;
    cityPickerView.showsSelectionIndicator=YES;
    cityPickerView.dataSource = self;
    cityPickerView.delegate = self;
    [alert.view addSubview:cityPickerView];
    [alert addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler: nil]];
    [alert addAction: [UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.chooseCitylabel.text = fanhuiDic[@"name"];
        self.chooseCitylabel.textColor = [UIColor blackColor];
        [valueDic setValue:fanhuiDic[@"code"] forKey:@"areacode"];
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}

//选择所属派出所
- (void)choosePoliceAction{
    LKInfoLog(@"选择所属派出所");
    if (((NSString *)valueDic[@"areacode"]).length == 0) {
        [self showErrorWithTitle:@"请先选择行政区" autoCloseTime:2];
    }else{
        policeArray = [NSMutableArray array];
        [SearchResidencePermitViewModel searchPoliceWithAreaId:fanhuiDic[@"id"] whenSuccess:^(id responseObject) {
            [policeArray addObjectsFromArray:responseObject[@"data"]];
            [policeArray insertObject:@{@"code":@"",@"name":@"无限制"} atIndex:0];
            [policePickerView reloadAllComponents];
            policeFanhuiDic = policeArray[0];
        } fail:^(NSError *error) {
        }];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle: nil message: @"请选择所属派出所\n\n\n\n\n\n\n\n\n"  preferredStyle: UIAlertControllerStyleActionSheet];
        policePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, GETWIDTH-20, 180)];
        // 显示选中框
        policePickerView.showsSelectionIndicator=YES;
        policePickerView.dataSource = self;
        policePickerView.delegate = self;
        [alert.view addSubview:policePickerView];
        [alert addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler: nil]];
        [alert addAction: [UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.choosePoliceLabel.text = policeFanhuiDic[@"name"];
            self.choosePoliceLabel.textColor = [UIColor blackColor];
            [valueDic setValue:policeFanhuiDic[@"code"] forKey:@"pcscode"];
        }]];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

//列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

//每列的个数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (pickerView.tag == 10086) {
        return cityNameArray.count;
    }else if (pickerView.tag == 10010){
        return conditionArray.count;
    }
    else{
        return policeArray.count;
    }
}

//返回选中的行
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (pickerView.tag == 10086) {
        return [cityNameArray objectAtIndex:row][@"name"];
    }else if (pickerView.tag == 10010){
        return [conditionArray objectAtIndex:row];
    }
    else{
         return [policeArray objectAtIndex:row][@"name"];;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (pickerView.tag == 10086) {
        fanhuiDic = cityNameArray[row];
    }else if (pickerView.tag == 10010){
        conditionString = conditionArray[row];
    }
    else{
        policeFanhuiDic = policeArray[row];
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        pickerLabel.minimumFontSize = 8.;
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
    }
    // Fill the label text here
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}


//选择审核状态
- (void)chooseCheckStatusAction{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选择审核状态" preferredStyle: UIAlertControllerStyleActionSheet];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *shenHeStatus = [UIAlertAction actionWithTitle:@"无限制" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.chooseCheckStatusLabel.text = @"无限制";
        self.chooseCheckStatusLabel.textColor = [UIColor blackColor];
        [valueDic setValue:@"" forKey:@"status"];
    }];
    UIAlertAction *officerAction = [UIAlertAction actionWithTitle:@"待审核" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.chooseCheckStatusLabel.text = @"待审核";
        self.chooseCheckStatusLabel.textColor = [UIColor blackColor];
        [valueDic setValue:@"1" forKey:@"status"];
    }];
    UIAlertAction *policeAction = [UIAlertAction actionWithTitle:@"已通过" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.chooseCheckStatusLabel.text = @"已通过";
        self.chooseCheckStatusLabel.textColor = [UIColor blackColor];
        [valueDic setValue:@"2" forKey:@"status"];
    }];
    UIAlertAction *HKMAction = [UIAlertAction actionWithTitle:@"已拒绝" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.chooseCheckStatusLabel.text = @"已拒绝";
        self.chooseCheckStatusLabel.textColor = [UIColor blackColor];
        [valueDic setValue:@"3" forKey:@"status"];
    }];
    [alertController addAction:shenHeStatus];
    [alertController addAction:cancelAction];
    [alertController addAction:officerAction];
    [alertController addAction:policeAction];
    [alertController addAction:HKMAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

//选择申请条件
- (void)chooseApplyConditionAction{
    LKLog(@"申请条件");
    conditionArray= [[NSArray alloc]initWithObjects:@"居住证换领",@"随迁子女在我市全日制中小学连续就读",@"取得职业学校或普通高等学校学籍并连续就读的",@"在大连市连续交纳社保1年以上",@"在本地经营企业",@"在本市驾校学习",@"有劳动合同并交纳3个月以上社保",@"政府机关或事业单位人员",@"与本地户籍人口结婚",@"买房",@"在大连市居住满半年", nil];
    conditionString = [conditionArray objectAtIndex:0];
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle: nil message: @"请选择申请条件\n\n\n\n\n\n\n\n\n"  preferredStyle: UIAlertControllerStyleActionSheet];
    conditionPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, GETWIDTH-20, 180)];
    // 显示选中框
    conditionPickerView.showsSelectionIndicator=YES;
    conditionPickerView.dataSource = self;
    conditionPickerView.delegate = self;
    conditionPickerView.tag = 10010;
    [alert.view addSubview:conditionPickerView];
    [alert addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler: nil]];
    [alert addAction: [UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.chooseApplyConditionLabel.text = conditionString;
        self.chooseApplyConditionLabel.textColor = [UIColor blackColor];
        if ([conditionString isEqualToString:@"无"]) {
            [valueDic setValue:@"" forKey:@"condition"];
        }else{
            [valueDic setValue:[NSString stringWithFormat:@"%ld",[conditionArray indexOfObject:conditionString] + 1] forKey:@"condition"];
        }
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}

//选择房屋权属
- (void)chooseHouseAffiliationAction{
    LKInfoLog(@"房屋权属");
//    1:租赁房屋;2:自有房屋:3:学校或单位宿舍;
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选择房屋权属" preferredStyle: UIAlertControllerStyleActionSheet];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *buxianAction = [UIAlertAction actionWithTitle:@"无限制" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.chooseHouseAffiliationLabel.text = @"无限制";
        self.chooseHouseAffiliationLabel.textColor = [UIColor blackColor];
        [valueDic setValue:@"" forKey:@"dwellingtype"];
    }];
    UIAlertAction *zulinAction = [UIAlertAction actionWithTitle:@"租赁房屋" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.chooseHouseAffiliationLabel.text = @"租赁房屋";
        self.chooseHouseAffiliationLabel.textColor = [UIColor blackColor];
        [valueDic setValue:@"1" forKey:@"dwellingtype"];
    }];
    UIAlertAction *ziyouAction = [UIAlertAction actionWithTitle:@"自有房屋" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.chooseHouseAffiliationLabel.text = @"自有房屋";
        self.chooseHouseAffiliationLabel.textColor = [UIColor blackColor];
        [valueDic setValue:@"2" forKey:@"dwellingtype"];
    }];
    UIAlertAction *schoolAction = [UIAlertAction actionWithTitle:@"学校或单位宿舍" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.chooseHouseAffiliationLabel.text = @"学校或单位宿舍";
        self.chooseHouseAffiliationLabel.textColor = [UIColor blackColor];
        [valueDic setValue:@"3" forKey:@"dwellingtype"];
    }];
    [alertController addAction:buxianAction];
    [alertController addAction:cancelAction];
    [alertController addAction:zulinAction];
    [alertController addAction:ziyouAction];
    [alertController addAction:schoolAction];
  
    [self presentViewController:alertController animated:YES completion:nil];
}


//提交按钮
- (void)submitButtonAction{
    [valueDic setValue:self.nameTextfield.text forKey:@"name"];
    [valueDic setValue:self.indentityNumberTextfield.text forKey:@"idno"];
    NSLog(@"字典是  %@",valueDic);
    QueryResultResidencePermitViewController *tempVC = [[QueryResultResidencePermitViewController alloc]init];
    tempVC.valueDic = valueDic;
    [LKFreeCtrl pushViewController:tempVC];
//    if (((NSString *)valueDic[@"areacode"]).length > 0) {
//        
//    }else{
//        [self showErrorWithTitle: @"请选择区域" autoCloseTime: 3];
//    }
}

- (void)resetButtonAction{
    self.chooseStartTimeLabel.text = @"请选择";
    self.chooseStartTimeLabel.textColor = LKColorMake(188, 188, 188, 1);
    self.chooseEndTimeLabel.text = @"请选择";
    self.chooseEndTimeLabel.textColor = LKColorMake(188, 188, 188, 1);
    self.chooseCitylabel.text = @"请选择";
    self.chooseCitylabel.textColor = LKColorMake(188, 188, 188, 1);
    self.choosePoliceLabel.text = @"请选择";
    self.choosePoliceLabel.textColor = LKColorMake(188, 188, 188, 1);
    self.chooseCheckStatusLabel.text = @"请先择";
    self.chooseCheckStatusLabel.textColor = LKColorMake(188, 188, 188, 1);
    self.chooseApplyConditionLabel.text = @"请选择";
    self.chooseApplyConditionLabel.textColor = LKColorMake(188, 188, 188, 1);
    self.chooseHouseAffiliationLabel.text = @"请选择";
    self.chooseHouseAffiliationLabel.textColor = LKColorMake(188, 188, 188, 1);
    self.nameTextfield.text = @"";
    self.indentityNumberTextfield.text = @"";
    
}

@end
